package com.lhkj.ct.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.meta.admin.model.entity.TblConfig;
import com.lhkj.ct.meta.admin.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-09-12
 */
@RestController
@RequestMapping("api/system/config")
@Api(tags = "11.参数配置")
@ApiSupport(order = 11)
@Validated
public class SysConfigController {

    @Resource
    private SysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @ApiOperation(value = "参数列表")
    @ApiOperationSupport(order = 1)
//    //@RequiresPermissions("system:config:list")
    @GetMapping("/list")
    public ActionResult<IPage<TblConfig>> list(TblConfig config, PageQuery pageQuery) {
        return ActionResult.success(configService.selectConfigPage(config, pageQuery));
    }

//    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:config:export')")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysConfig config)
//    {
//        List<SysConfig> list = configService.selectConfigList(config);
//        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
//        util.exportExcel(response, list, "参数数据");
//    }
//
    /**
     * 根据参数编号获取详细信息
     */
    @ApiOperation(value = "参数配置详情")
    @ApiOperationSupport(order = 2)
//    //@RequiresPermissions("system:config:query")
    @GetMapping(value = "/{configId}")
    public ActionResult<?> getInfo(@PathVariable String configId) {
        return ActionResult.success(configService.getById(configId));
    }

//
//    /**
//     * 根据参数键名查询参数值
//     */
//    @GetMapping(value = "/configKey/{configKey}")
//    public AjaxResult getConfigKey(@PathVariable String configKey)
//    {
//        return success(configService.selectConfigByKey(configKey));
//    }

    /**
     * 新增参数配置
     */
    @ApiOperation(value = "新增参数配置")
    @ApiOperationSupport(order = 3)
//    //@RequiresPermissions("system:config:add")
    @Log(title = "参数管理", actionType = ActionType.INSERT)
    @PostMapping
    public ActionResult<Void> add(@Validated @RequestBody TblConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return ActionResult.fail("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return ActionResult.judge(configService.insert(config));
    }

    /**
     * 修改参数配置
     */
    @ApiOperation(value = "修改参数配置")
    @ApiOperationSupport(order = 4)
    ////@RequiresPermissions("system:config:edit")
    @Log(title = "参数管理", actionType = ActionType.UPDATE)
    @PutMapping
    public ActionResult<Void> edit(@Validated @RequestBody TblConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            return ActionResult.fail("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        return ActionResult.judge(configService.updateById(config));
    }

    /**
     * 删除参数配置
     */
    @ApiOperation(value = "删除参数配置")
    @ApiOperationSupport(order = 5)
    ////@RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", actionType = ActionType.DELETE)
    @DeleteMapping("delete")
    public ActionResult<Void> remove(@Validated @NotEmpty(message = "参数不能为空") @RequestBody List<String> configIds) {
        configService.removeByIds(configIds);
        return ActionResult.success();
    }

    /**
     * 刷新参数缓存
     */
    @ApiOperation(value = "刷新参数缓存")
    @ApiOperationSupport(order = 6)
    ////@RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", actionType = ActionType.CLEAN)
    @DeleteMapping("/refreshCache")
    public ActionResult<Void> refreshCache() {
        configService.resetConfigCache();
        return ActionResult.success();
    }
}
